define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'book/index' + location.search,
                    add_url: 'book/add',
                    edit_url: 'book/edit',
                    del_url: 'book/del',
                    multi_url: 'book/multi',
                    import_url: 'book/import',
                    dragsort_url: '',
                    table: 'book',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'grade_id asc,year desc',
                fixedColumns: true,
                fixedRightNumber: 1,
	            searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'class_id', title: __('Class_id'), searchList: Config.classList,
                            formatter: function(value, row, index){
								return row.class_text;
                            }
                        },
                        {field: 'publisher_id', title: __('Publisher_id'), searchList: Config.publisherList,
                            formatter: function(value, row, index){
								return row.publisher_text;
                            }
                        },
                        {field: 'year', title: __('Year')},
                        {field: 'grade_id', title: __('Grade_id'), searchList: Config.gradeList, operate: '=',
	                        formatter: function(value, row, index){
								return row.grade_text;
	                        }
						},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'createtime', title: __('Createtime'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'status', title: __('Status'), searchList: Config.statusList, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
								{
                                    title: '单词管理',
                                    text: '单词管理',
	                                icon: 'fa fa-file-word-o',
	                                classname: 'btn btn-warning btn-xs btn-detail btn-dialog',
									extend: 'data-area=\'["80%","80%"]\'',
									url: function(row, column){
									    return "book.words/index/book_id/" + row.id;
									},
									visible:function(row, column){
										return row.class_id==3;
									},
                                },
								{
                                    title: '背单词',
                                    text: '背单词',
	                                icon: 'fa fa-book',
	                                classname: 'btn btn-info btn-xs btn-detail',
									extend: 'target="_blank"',
									url: function(row, column){
									    return "book.words/memory/book_id/" + row.id;
									},
									visible:function(row, column){
										return row.class_id==3;
									},
                                },
								{
                                    title: '单元管理',
                                    text: '单元管理',
	                                icon: 'fa fa-list',
	                                classname: 'btn btn-primary btn-xs btn-detail btn-dialog',
									extend: 'data-area=\'["80%","80%"]\'',
									url: function(row, column){
									    return "book.unit/index/book_id/" + row.id;
									},
                                },
                            ]
						}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

			// 修改弹窗尺寸
			/*$(".btn-add").data("area", ["80%", "80%"]);
			table.on('post-body.bs.table', function (e, settings, json, xhr) {
			    $(".btn-editone").data("area", ["80%", "80%"]);
			});*/
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'book/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'book/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'book/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Form.api.bindevent($("form[role=form]"), function (data, ret) {
				if(ret.url != ''){
					location.href = Fast.api.fixurl(ret.url);
					return false;
				}
            });
        },
        edit: function () {
            Controller.api.bindevent();

			// 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'book.files/index/book_id/'+book_id,
                    add_url: 'book.files/add/book_id/'+book_id,
                    edit_url: '',
                    del_url: 'book.files/del/book_id/'+book_id,
                    multi_url: '',
                    table: ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'id',
                columns: [
                    [
                        {field: 'id', title: __('Id')},
                        {field: 'admin.nickname', title: __('上传人'), operate: false},
                        {field: 'admin_id', title: __('上传人'), visible: false, addClass: "selectpage", extend: "data-source='auth/admin/index' data-field='nickname'"},
                        {field: 'preview', title: __('预览'), formatter: Controller.api.formatter.thumb, operate: false},
                        {field: 'filename', title: __('文件名称'), operate: false},
                        {field: 'url', title: __('文件地址'), formatter: Controller.api.formatter.url, visible: false},
                        {
                            field: 'create_time',
                            title: __('Createtime'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ],
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            //上传附件
            $("#c-third").on("change",function () {
                var url=$(this).val();
                if (url){
                    $(this).val("");
                    Fast.api.ajax({
                        url: $.fn.bootstrapTable.defaults.extend.add_url,
                        data: {url:url}
                    },function (data) {
                        table.bootstrapTable('refresh', {pageNumber: 1});
                    });
                }
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                thumb: function (value, row, index) {
                    var reg=/.*(\.png|\.jpg|\.jpeg|\.gif)$/;
                    if (reg.test(row.fullurl)) {
                        return '<a href="' + row.fullurl + '" target="_blank"><img src="' + row.fullurl  + '" alt="" style="max-height:90px;max-width:120px"></a>';
                    } else {
                        return '<a href="' + row.fullurl + '" target="_blank"><img src="' + Fast.api.fixurl("ajax/icon") + "?suffix=" + row.imagetype + '" alt="" style="max-height:90px;max-width:120px"></a>';
                    }
                },
                url: function (value, row, index) {
                    return '<a href="' + row.fullurl + '" target="_blank" class="label bg-green">' + row.url + '</a>';
                },
                filename: function (value, row, index) {
                    return '<div style="width:180px;margin:0 auto;text-align:center;overflow:hidden;white-space: nowrap;text-overflow: ellipsis;">' + Table.api.formatter.search.call(this, value, row, index) + '</div>';
                },
            }
        }
    };
    return Controller;
});
